/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "common/Utils.h"
#include "ParseParamVOneFive.h"


namespace backint {
std::vector<std::string> backupType {"backup", "restore", "inquire", "delete"};

std::vector<std::string> backupLevel {"COMPLETE", "DIFFERENTIAL", "INCREMENTAL", "METADATA", \
                                      "INITIALVOLUME", "CATALOG", "LOG"};

uint32_t ParseParamVOneFive::ParamParse(int argc, char** argv)
{
    int index = -1;
    const char *optstring = "u:f:p:i:o:s:c:l:v:V:";
    while ((index = getopt(argc, argv, optstring)) != -1) {
        switch (index) {
            case 'u':
                m_stuParam.m_userId = optarg;
                break;
            case 'f':
                m_stuParam.m_funType = optarg;
                break;
            case 'p':
                m_stuParam.m_parFile = optarg;
                break;
            case 'i':
                m_stuParam.m_inputFile = optarg;
                break;
            case 'o':
                m_stuParam.m_outFile = optarg;
                break;
            case 's':
                m_stuParam.m_dtBackupId = optarg;
                break;
            case 'c':
                m_stuParam.m_numberOfObj = Module::SafeStoi(optarg);
                break;
            case 'l':
                m_stuParam.m_backupType = optarg;
                break;
            case 'v':
                m_stuParam.m_version = optarg;
                break;
            case 'V':
                m_stuParam.m_versionDetail = optarg;
                break;
            default:
                ERRLOG("Error input data: %s\n", optarg);
                return FAILED;
        }
    }
    return SUCCESS;
}

uint32_t ParseParamVOneFive::ParamJudgment(StuParam &taskInfo)
{
    if (taskInfo.m_userId.empty() || taskInfo.m_parFile.empty() || taskInfo.m_inputFile.empty() \
        || taskInfo.m_outFile.empty()) {
        ERRLOG("The mandatory parameter is empty, userId: %s, parFile: %s, inputFile: %s, outFile: %s", \
               taskInfo.m_userId.c_str(), taskInfo.m_parFile.c_str(), taskInfo.m_inputFile.c_str(), \
               taskInfo.m_outFile.c_str());
        return FAILED;
    }

    auto typeIt = std::find(backupType.begin(), backupType.end(), taskInfo.m_funType);
    if (typeIt == backupType.end()) {
        ERRLOG("The parameter of funtion is incorrect");
        return FAILED;
    }

    if (taskInfo.m_funType == "backup") {
        auto levelIt = std::find(backupLevel.begin(), backupLevel.end(), taskInfo.m_backupType);
        if (levelIt == backupLevel.end()) {
            ERRLOG("The param of backupLevel is needed");
            return FAILED;
        }
        if (taskInfo.m_numberOfObj && taskInfo.m_dtBackupId.empty()) {
            ERRLOG("Param -c need combination with option -s");
            return FAILED;
        }
    }
    return SUCCESS;
}

ParseParamVOneFive::~ParseParamVOneFive()
{
}
}